<!--#
layout("/layouts/platform.html"){
#-->
<div id="app" v-cloak>
    <el-row class="header navbar">
        <div class="pull-right offscreen-right" style="margin-right: 20px;line-height: 50px;">
            <el-tooltip class="item" effect="dark" content="添加配置项">
                <el-button type="primary" icon="el-icon-plus"  size="mini" @click="openAdd" circle></el-button>
            </el-tooltip>
        </div>
    </el-row>
    <el-row class="el-table-container">
        <el-table
                :data="tableData" border
                @sort-change='pageOrder'
                size="small"
                header-align="center"
                style="width: 100%">
            <el-table-column
                    sortable
                    prop="configKey"
                    label="参数名"
                    header-align="center"
                    width="200">
            </el-table-column>
            <el-table-column
                    prop="configValue"
                    label="参数值"
                    header-align="center"
                    :show-overflow-tooltip="true"
                    width="200">
            </el-table-column>
            <el-table-column
                    prop="note"
                    header-align="center"
                    :show-overflow-tooltip="true"
                    label="说明">
            </el-table-column>
            <el-table-column
                    prop="note"
                    label="操作"
                    width="120">
                <template slot-scope="scope">
                    <el-dropdown @command="dropdownCommand">
                        <el-button size="mini">
                            <i class="ti-settings"></i>
                            <span class="ti-angle-down"></span>
                        </el-button>
                        <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item :command="{type:'edit',id:scope.row.configKey}">
                                修改
                            </el-dropdown-item>
                            <el-dropdown-item :command="{type:'delete',id:scope.row.configKey}"
                                              v-if="scope.row.configKey.indexOf('App')<0">
                                删除
                            </el-dropdown-item>
                        </el-dropdown-menu>
                    </el-dropdown>
                </template>
            </el-table-column>
        </el-table>
    </el-row>
    <el-row class="el-pagination-container">
        <el-pagination
                @size-change="pageSizeChange"
                @current-change="pageNumberChange"
                :current-page="pageForm.pageNumber"
                :page-sizes="[10, 20, 30, 50]"
                :page-size="pageForm.pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="pageForm.totalCount">
        </el-pagination>
    </el-row>
    <el-dialog :close-on-click-modal="false"
            title="新增参数"
            :visible.sync="addDialogVisible"
            width="40%">
        <el-form :model="formData" ref="addForm" :rules="formRules" label-width="80px">
            <el-form-item prop="configKey" label="参数名">
                <el-input maxlength="100" placeholder="参数名"
                          v-trim v-model="formData.configKey"
                          auto-complete="off" tabindex="1" type="text"></el-input>
            </el-form-item>
            <el-form-item prop="configValue" label="参数值">
                <el-input maxlength="100" placeholder="参数值"
                          v-trim v-model="formData.configValue"
                          auto-complete="off" tabindex="2" type="text"></el-input>
            </el-form-item>
            <el-form-item prop="note" label="说 明">
                <el-input type="textarea" v-trim v-model="formData.note"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
                    <el-button @click="addDialogVisible = false">取 消</el-button>
                    <el-button type="primary" @click="doAdd">确 定</el-button>
                  </span>
    </el-dialog>
    <el-dialog :close-on-click-modal="false"
            title="修改参数"
            :visible.sync="editDialogVisible"
            width="40%">
        <el-form :model="formData" ref="editForm" :rules="formRules" label-width="80px">
            <el-form-item prop="configKey" label="参数名">
                <el-input maxlength="100" placeholder="参数名"
                          v-trim v-model="formData.configKey"
                          auto-complete="off" tabindex="1" type="text" disabled></el-input>
            </el-form-item>
            <el-form-item prop="configValue" label="参数值">
                <el-input maxlength="100" placeholder="参数值"
                          v-trim v-model="formData.configValue"
                          auto-complete="off" tabindex="2" type="text"></el-input>
            </el-form-item>
            <el-form-item prop="note" label="说 明">
                <el-input type="textarea" v-trim v-model="formData.note"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
                    <el-button @click="editDialogVisible = false">取 消</el-button>
                    <el-button type="primary" @click="doEdit">确 定</el-button>
                  </span>
    </el-dialog>
</div>
<script>
    new Vue({
        el: "#app",
        data: function () {
            return {
                addDialogVisible: false,
                editDialogVisible: false,
                tableData: [],
                pageForm:{
                    pageSize: 10,
                    pageNumber: 1,
                    pageOrderName: "",
                    pageOrderBy: "",
                    totalCount: 0,
                },
                formData: {
                    configKey: "",
                    configValue: "",
                    note: ""
                },
                formRules: {
                    configKey: [
                        {required: true, message: '请输入参数名', trigger: ['blur', 'change']},
                    ],
                    configValue: [
                        {required: true, message: '请输入参数值', trigger: ['blur', 'change']},
                    ],
                }
            }
        },
        methods: {
            openAdd: function () {
                this.addDialogVisible = true;
                this.formData = {};//打开新增窗口,表单先清空
            },
            doAdd: function () {
                var self = this;
                var url = base + "/platform/sys/conf/addDo";
                self.$refs["addForm"].validate(function (valid) {
                    if (valid) {//表单验证通过
                        $.post(url, self.formData, function (data) {
                            if (data.code == 0) {
                                self.$message({
                                    message: data.msg,
                                    type: 'success'
                                });
                                self.addDialogVisible = false;
                                self.pageData();//重新加载分页数据
                            } else {
                                self.$message({
                                    message: data.msg,
                                    type: 'error'
                                });
                            }
                        }, "json");
                    }
                });
            },
            doEdit: function () {
                var self = this;
                var url = base + "/platform/sys/conf/editDo";
                self.$refs["editForm"].validate(function (valid) {
                    if (valid) {
                        $.post(url, self.formData, function (data) {
                            if (data.code == 0) {
                                self.$message({
                                    message: data.msg,
                                    type: 'success'
                                });
                                self.editDialogVisible = false;
                                self.pageData();//重新加载分页数据
                            } else {
                                self.$message({
                                    message: data.msg,
                                    type: 'error'
                                });
                            }
                        }, "json");
                    }
                });
            },
            pageOrder: function (column) {//按字段排序
                this.pageForm.pageOrderName = column.prop;
                this.pageForm.pageOrderBy = column.order;
                this.pageData();
            },
            pageNumberChange: function (val) {//页码更新操作
                this.pageForm.pageNumber = val;
                this.pageData();
            },
            pageSizeChange: function (val) {//分页大小更新操作
                this.pageForm.pageSize = val;
                this.pageData();
            },
            pageData: function () {//加载分页数据
                var self = this;
                sublime.showLoadingbar();//显示loading
                $.post(base + "/platform/sys/conf/data", self.pageForm, function (data) {
                    sublime.closeLoadingbar();//关闭loading
                    if (data.code == 0) {
                        self.tableData = data.data.list;
                        self.pageForm.totalCount = data.data.totalCount;
                    } else {
                        self.$message({
                            message: data.msg,
                            type: 'error'
                        });
                    }
                }, "json");
            },
            dropdownCommand: function (command) {//监听下拉框事件
                var self = this;
                if ("edit" == command.type) {
                    $.post(base + "/platform/sys/conf/edit/" + command.id, {}, function (data) {
                        if (data.code == 0) {
                            self.formData = data.data;//加载后台表单数据
                            self.editDialogVisible = true;//打开编辑窗口
                        } else {
                            self.$message({
                                message: data.msg,
                                type: 'error'
                            });
                        }
                    }, "json");
                }
                if ("delete" == command.type) {
                    self.$confirm('此操作将删除 ' + command.id, '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning',
                        callback: function (a, b) {
                            if ("confirm" == a) {//确认后再执行
                                $.post(base + "/platform/sys/conf/delete/" + command.id, {}, function (data) {
                                    if (data.code == 0) {
                                        self.$message({
                                            message: data.msg,
                                            type: 'success'
                                        });
                                        self.pageData();
                                    } else {
                                        self.$message({
                                            message: data.msg,
                                            type: 'error'
                                        });
                                    }
                                }, "json");
                            }
                        }
                    });
                }
            }
        },
        created: function () {
            this.pageData();
        }
    });
</script>
<!--#
}
#-->
